﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using MpSoft.Library.DTO;
using MpSoft.Library.BUSS.Categories;
using MpSoft.Library.CommonLibrary;
using System.Data;
using System.Xml.Serialization;
using System.Xml;
using MpSoft.Library.BUSS.Settings;

namespace MpSoft.Web
{
    public partial class Default : System.Web.UI.Page
    {

        #region "Private Data"

                private int itemID = -1;
                private string categoryCode = string.Empty;
                private string ControlType = string.Empty;
                private string Title = string.Empty;

        #endregion

        #region "Private Method"
        
                private void CreateInfoMetaTag(string typecontrol, string CategorySeName,string _title, int ItemID, Category objCate)
                {
                    var objSiteInfo = SeoSiteInfoBUSS.SeoSite(typecontrol, _title, CategorySeName, itemID, objCate);
                    //Thẻ title---------------------
                    HtmlTitle title = new HtmlTitle();
                    title.Text = objSiteInfo.Title;
                    this.Page.Header.Controls.Add(title);
                    //Thẻ Meta keyword--------------
                    HtmlMeta meta = null;
                    meta = new HtmlMeta();
                    meta.Name = "keywords";
                    meta.Content = objSiteInfo.Keywords;
                    this.Page.Header.Controls.Add(meta);
                    //Thẻ Meta Description-----------
                    meta = new HtmlMeta();
                    meta.Name = "description";
                    meta.Content = objSiteInfo.Descriptions;
                    this.Page.Header.Controls.Add(meta);
                }

                private void Router()
                {
                    string ControlName = "";
                    var objCate = CategoryBUSS.Category_BySeName(categoryCode);
                    List<string> lstCategory = new List<string>();
                    lstCategory.Add("tim-kiem");
                    lstCategory.Add("thanh-toan");
                    lstCategory.Add("dang-nhap");
                    lstCategory.Add("dang-ky");
                    lstCategory.Add("logout");
                    lstCategory.Add("quen-mat-khau");
                    lstCategory.Add("login-success");
                    lstCategory.Add("myprofile");
                    lstCategory.Add("myorder");
                    lstCategory.Add("xac-nhan-thanh-toan");
                    lstCategory.Add("thanh-toan-thanh-cong");
                    lstCategory.Add("dang-ky-thanh-cong");
                    lstCategory.Add("tham-do-y-kien");
                    //lstCategory.Add("google-map");
                    lstCategory.Add("ua-thich");
                    lstCategory.Add("mycomment");
                    lstCategory.Add("lien-he");
                    lstCategory.Add("gio-hang");
                    lstCategory.Add("xac-nhan-thanh-toan");
                    //lstCategory.Add("loc-hang");
                    lstCategory.Add("tim-kiem-tin");
                    lstCategory.Add("tim-kiem-san-pham");
                    foreach (string strCategory in lstCategory)
                    {
                        if (categoryCode.Equals(strCategory))
                        {
                            ControlType = categoryCode;
                            break;
                        }
                    }
                    if (ControlType.Equals("") && objCate != null)
                        ControlType = objCate.TypeCode;
                    if (!string.IsNullOrEmpty(categoryCode) && string.IsNullOrEmpty(ControlType))
                        Response.Redirect("/");
                    CreateInfoMetaTag(ControlType, categoryCode, Title, itemID, objCate);
                    if (itemID > 0)
                    {
                        switch (ControlType)
                        {
                            case "tin-tuc":
                                ControlName = "News/NewsDetailControl.ascx";
                                break;
                            case "bai-viet":
                                ControlName = "ArticleControl.ascx";
                                break;
                            case "san-pham":
                                ControlName = "Product/ProductDetailControl.ascx";
                                break;
                        }
                    }
                    else
                    {
                        switch (ControlType)
                        {
                            //case "google-map":
                            //    ControlName = "MapControl.ascx";
                            //    break;
                            case "thanh-toan":
                                ControlName = "CartControl.ascx";
                                break;
                            case "gio-hang":
                                ControlName = "Customer/CartControl.ascx";
                                break;
                            case "xac-nhan-thanh-toan":
                                ControlName = "Customer/OrderDetailControl.ascx";
                                break;
                            case "thong-tin-tai-khoan":
                                ControlName = "AccountInfoControl.ascx";
                                break;
                            case "dang-nhap":
                                ControlName = "LoginControl.ascx";
                                break;
                            case "quen-mat-khau":
                                ControlName = "ForgetPasswordControl.ascx";
                                break;
                            case "dang-ky":
                                ControlName = "RegisterControl.ascx";
                                break;
                            case "login-success":
                                ControlName = "LoginSuccessControl.ascx";
                                break;
                            case "dang-ky-thanh-cong":
                                ControlName = "RegisterSuccessControl.ascx";
                                break;
                            case "myprofile":
                                ControlName = "MyProfileControl.ascx";
                                break;
                            case "myorder":
                                ControlName = "MyOrderControl.ascx";
                                break;
                            case "san-pham":
                                ControlName = "Product/ProductsControl.ascx";
                                break;
                            case "tin-tuc":
                                ControlName = "News/NewsControl.ascx";
                                break;
                            case "aboutcontrol":
                                ControlName = "AboutControl.ascx";
                                break;
                            case "bai-viet":
                                ControlName = "Article/ArticleControl.ascx";
                                break;
                            case "download":
                                ControlName = "DownloadControl.ascx";
                                break;
                            case "lien-ket":
                                ControlName = "LinkControl.ascx";
                                break;
                            case "tim-kiem-san-pham":
                                ControlName = "Product/ProductSearch.ascx";
                                break;
                            case "tim-kiem-tin":
                                ControlName = "News/SearchNewsControl.ascx";
                                break;
                            case "lien-he":
                                ControlName = "Customer/ContactControl.ascx";
                                break;
                            case "galery":
                                ControlName = "GaleryImageControl.ascx";
                                break;
                            case "so-do-web":
                                ControlName = "SiteMapControl.ascx";
                                break;
                            case "tham-do-y-kien":
                                ControlName = "Customer/PollResultControl.ascx";
                                break;
                            case "ua-thich":
                                ControlName = "WishlistControl.ascx";
                                break;
                            case "mycomment":
                                ControlName = "MyCommentControl.ascx";
                                break;
                            case "thanh-toan-thanh-cong":
                                ControlName = "PaySuccess.ascx";
                                break;
                            case "logout":
                                ControlName = "LogoutControl.ascx";
                                break;
                            default:
                                ControlName = "AllPage/HomeControl.ascx";
                                break;
                        }
                    }
                    if (categoryCode.ToLowerInvariant().Equals("default") || string.IsNullOrEmpty(categoryCode))
                        Layout_2C.UserControl = "AllPage/HomeControl.ascx";
                    else
                        Layout_2C.UserControl = ControlName;
           
                }

        #endregion

        #region "Event Handle"

        protected void Page_Load(object sender, EventArgs e)
        {
            //LanguageCode = LanguageInfo.GetLanguage();
            if (!string.IsNullOrEmpty(Request.QueryString["CategoryCode"]))
                categoryCode = Request.QueryString["CategoryCode"].ToString().Trim();
            if (!string.IsNullOrEmpty(Request.QueryString["ItemID"]))
                int.TryParse(Request.QueryString["ItemID"].ToString(), out itemID);
            if (!string.IsNullOrEmpty(Request.QueryString["Title"]))
                Title = Request.QueryString["Title"].ToString().Trim();
            //this.DataBind();
            Router();
        }

#endregion                        
        
        #region Properties

        public string CategoryCode { get { return categoryCode; } }
        public int ItemID { get { return itemID; } }

        #endregion

    }
}
